package Mash.Utility;
/*Copyright (c)  Sergey Schekochikhin (Genocidicbunny)
 * created on Sep 30, 2009 11:16:51 PM
 * Reads in a map from a file.
 * Format is as follows:
 * row
 * col
 * list of terraintypes delimited by comma
 * list of terraintypes delimited by comma
 */
 import Mash.Map.TerrainType;

 import java.io.BufferedReader;
 import java.io.FileReader;
 import java.io.IOException;
 import java.io.File;

public class TerrainMapRead {
	private static BufferedReader mapRead;
	public static TerrainType[][] readInTerrainMap(String fileLoc) throws IOException{
		try{System.out.println(Thread.currentThread().getContextClassLoader().getResource(fileLoc));}catch(Exception e){e.printStackTrace();}
		try{

			mapRead = new BufferedReader(new FileReader(new File(Thread.currentThread().getContextClassLoader().getResource(fileLoc).toURI())));

		}catch(Exception e){System.out.println("File " + fileLoc + " not found."); e.printStackTrace();}
		int row = Integer.parseInt(mapRead.readLine());
		int col = Integer.parseInt(mapRead.readLine());
		TerrainType[][] ttype = new TerrainType[row][col];
		String[] tokens;
		String line;
		int currRow = 0;
		while((line = mapRead.readLine()) != null){
			 tokens = line.split(",");
			for(int i = 0; i < tokens.length; i++){
				ttype[currRow][i] = chooseTerrain(Integer.parseInt(tokens[i]));
			}
			currRow++;
			
		}
		return ttype;
	}
	//Chooses the appropriate terrain to the given int.
	private static TerrainType chooseTerrain(int x){
		switch(x){
			case 0: return TerrainType.GRASSLAND;
			case 1: return TerrainType.ROAD;
			case 2: return TerrainType.BRIDGE;
			case 3: return TerrainType.COAST_SHALLOWS;
			case 4: return TerrainType.FOREST;
			case 5: return TerrainType.MOUNTAIN;
			case 6: return TerrainType.OCEAN;
		}
		return null;
	}

}
